package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountLockResultVo;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;

	@GuiGuLogin
	@GetMapping(value = "/getAvailableAmount")
	public Result<BigDecimal> getAvailableAmount() {
		BigDecimal availableAmount = userAccountService.getAvailableAmount();
		return Result.build(availableAmount , ResultCodeEnum.SUCCESS) ;
	}

	/**
	 * 检查并锁定账户的可用金额
	 */
	@PostMapping(value = "/checkAndLock")
	public Result<AccountLockResultVo> checkAndLock(@RequestBody AccountLockVo accountLockVo) {
		AccountLockResultVo accountLockResultVo = userAccountService.checkAndLock(accountLockVo) ;
		return Result.build(accountLockResultVo , ResultCodeEnum.SUCCESS) ;
	}

	@GuiGuLogin
	@GetMapping(value = "/findUserRechargePage/{pageNo}/{pageSize}")
	public Result<IPage<UserAccountDetail>> findUserRechargePage(@PathVariable(value = "pageNo") Integer pageNo ,
																 @PathVariable(value = "pageSize") Integer pageSize) {
		String tradeType = SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT ;
		IPage<UserAccountDetail> page = userAccountService.findUserRechargePage(pageNo , pageSize , tradeType) ;
		return Result.build(page , ResultCodeEnum.SUCCESS) ;
	}

	@GuiGuLogin
	@GetMapping(value = "/findUserConsumePage/{pageNo}/{pageSize}")
	public Result<IPage<UserAccountDetail>> findUserConsumePage(@PathVariable(value = "pageNo") Integer pageNo ,
																 @PathVariable(value = "pageSize") Integer pageSize) {
		String tradeType = SystemConstant.ACCOUNT_TRADE_TYPE_MINUS ;
		IPage<UserAccountDetail> page = userAccountService.findUserRechargePage(pageNo , pageSize , tradeType) ;
		return Result.build(page , ResultCodeEnum.SUCCESS) ;
	}

	@GetMapping(value = "/initAccount/{userId}")
	public Result initAccount(@PathVariable(value = "userId") Long userId) {
		userAccountService.initAccount(userId) ;
		return Result.build(null , ResultCodeEnum.SUCCESS) ;
	}
}

